-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add RequestPathPattern method to Typhon Request #169
Conversation
return "" | ||
} | ||
|
||
// RequestMethod returns the HTTP method of the request |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not particularly keen on the name of this so any suggestions would be appreciated 🙇 . I'm also wondering if RouterEndpointMethod (which could be a "*") would be more beneficial here...
request.go
Outdated
@@ -213,6 +213,22 @@ func (r Request) ResponseWithCode(body interface{}, statusCode int) Response { | |||
return rsp | |||
} | |||
|
|||
// RouterEndpointPattern finds the router pattern that matches the request. This is only callable while the request | |||
// is being served. | |||
func (r Request) RouterEndpointPattern() string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RequestMethod()
feels intuitive and simple given we can't use Method()
. 👍
For this method, RouterEntryPattern()
is probably the most correct, but would RequestPathPattern()
or even RequestPattern()
be simpler and consistent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed to RequestPathPattern()
in e9f9213. I suspect that RequestPattern by itself would require some background context to understand
request.go
Outdated
if router := RouterForRequest(r); router != nil { | ||
if pathPattern := router.Pattern(r); pathPattern != "" { | ||
return pathPattern | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative implementation here would be to store the pattern used to serve the request in the context, as we do with the router currently. Then we don't need to look up the pattern separately (which could theoretically have changed if a new route is registered in the intervening time).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have changed it to store instead in e9f9213 👍
bb1576e
to
e9f9213
Compare
router.go
Outdated
routerContextKey = routerContextKeyType{} | ||
routerComponentsRe = regexp.MustCompile(`(?:^|/)(\*\w*|:\w+)`) | ||
routerContextKey = routerContextKeyType{} | ||
routerPathContextKey = routerPathContextKeyType{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
routerPathContextKey = routerPathContextKeyType{} | |
routerRequestPatternContextKey = routerRequestPatternContextKeyType{} |
Optional: This might be clearer to distinguish between path and pattern?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed in 776b548 👍
router.go
Outdated
@@ -44,6 +46,13 @@ func RouterForRequest(r Request) *Router { | |||
return nil | |||
} | |||
|
|||
func routerEntryPathPatternForRequest(r Request) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func routerEntryPathPatternForRequest(r Request) string { | |
func routerPathPatternForRequest(r Request) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed in fc0f840 👍
📚 Background
We have found several use cases such as metrics, logging, and authorization where knowing the router path (without any dynamic parameters) is useful to avoid unbounded cardinality or make searching for a particular value easier. At the moment, it is doable to get this but it's not easy.
🆕 What does this PR do?
RouterEndpointPattern
method to get the router endpoint patternRequestMethod
method to get the request method. This is needed to fulfil an interface between the two APIs. I couldn't name it justMethod
as this clashes with the string inside the contained http.Request object🛡️ Testing
Added new unittests.